//
//  ProfileSubPages.swift
//  WeChatClient
//
//  Created by AI Assistant on 2025/10/31.
//

import SwiftUI

// MARK: - 支付页面
struct PaymentView: View {
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(spacing: 0) {
                    // 余额卡片
                    VStack(spacing: 16) {
                        HStack {
                            VStack(alignment: .leading, spacing: 4) {
                                Text("零钱")
                                    .font(.system(size: 16))
                                    .foregroundColor(.secondary)
                                
                                Text("¥1,234.56")
                                    .font(.system(size: 24, weight: .medium))
                                    .foregroundColor(.primary)
                            }
                            
                            Spacer()
                            
                            Button("充值") {
                                // 充值功能
                            }
                            .padding(.horizontal, 16)
                            .padding(.vertical, 8)
                            .background(Color.green)
                            .foregroundColor(.white)
                            .clipShape(RoundedRectangle(cornerRadius: 16))
                        }
                        
                        Divider()
                        
                        HStack {
                            PaymentOptionView(icon: "creditcard.fill", title: "银行卡", color: .blue)
                            Spacer()
                            PaymentOptionView(icon: "qrcode", title: "收付款", color: .green)
                            Spacer()
                            PaymentOptionView(icon: "clock.fill", title: "账单", color: .orange)
                        }
                    }
                    .padding()
                    .background(Color.white)
                    .clipShape(RoundedRectangle(cornerRadius: 12))
                    .padding()
                    
                    // 服务列表
                    VStack(spacing: 0) {
                        PaymentServiceRow(icon: "phone.fill", title: "手机充值", color: .green)
                        Divider().padding(.leading, 60)
                        PaymentServiceRow(icon: "car.fill", title: "出行服务", color: .blue)
                        Divider().padding(.leading, 60)
                        PaymentServiceRow(icon: "film.fill", title: "电影票", color: .red)
                        Divider().padding(.leading, 60)
                        PaymentServiceRow(icon: "gamecontroller.fill", title: "游戏充值", color: .purple)
                    }
                    .background(Color.white)
                    .clipShape(RoundedRectangle(cornerRadius: 12))
                    .padding(.horizontal)
                }
            }
            .navigationTitle("支付")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("完成") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
        }
    }
}

struct PaymentOptionView: View {
    let icon: String
    let title: String
    let color: Color
    
    var body: some View {
        VStack(spacing: 8) {
            Image(systemName: icon)
                .font(.system(size: 24))
                .foregroundColor(color)
            
            Text(title)
                .font(.system(size: 12))
                .foregroundColor(.primary)
        }
    }
}

struct PaymentServiceRow: View {
    let icon: String
    let title: String
    let color: Color
    
    var body: some View {
        HStack(spacing: 15) {
            Image(systemName: icon)
                .font(.system(size: 20))
                .foregroundColor(color)
                .frame(width: 30)
            
            Text(title)
                .font(.system(size: 16))
                .foregroundColor(.primary)
            
            Spacer()
            
            Image(systemName: "chevron.right")
                .font(.system(size: 12))
                .foregroundColor(.secondary)
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 12)
    }
}

// MARK: - 收藏页面
struct FavoritesView: View {
    @Environment(\.presentationMode) var presentationMode
    
    private let favorites = [
        FavoriteItem(title: "有趣的文章", type: "链接", time: "昨天"),
        FavoriteItem(title: "好听的音乐", type: "音乐", time: "2天前"),
        FavoriteItem(title: "重要的笔记", type: "笔记", time: "1周前"),
        FavoriteItem(title: "美食图片", type: "图片", time: "2周前"),
        FavoriteItem(title: "学习视频", type: "视频", time: "1个月前")
    ]
    
    var body: some View {
        NavigationView {
            List {
                ForEach(favorites) { item in
                    FavoriteItemRow(item: item)
                }
            }
            .navigationTitle("我的收藏")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("完成") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
        }
    }
}

struct FavoriteItem: Identifiable {
    let id = UUID()
    let title: String
    let type: String
    let time: String
}

struct FavoriteItemRow: View {
    let item: FavoriteItem
    
    var body: some View {
        HStack(spacing: 12) {
            Image(systemName: getIconForType(item.type))
                .font(.system(size: 20))
                .foregroundColor(getColorForType(item.type))
                .frame(width: 40, height: 40)
                .background(getColorForType(item.type).opacity(0.1))
                .clipShape(RoundedRectangle(cornerRadius: 8))
            
            VStack(alignment: .leading, spacing: 4) {
                Text(item.title)
                    .font(.system(size: 16))
                    .foregroundColor(.primary)
                
                HStack {
                    Text(item.type)
                        .font(.system(size: 12))
                        .foregroundColor(.secondary)
                    
                    Text("•")
                        .font(.system(size: 12))
                        .foregroundColor(.secondary)
                    
                    Text(item.time)
                        .font(.system(size: 12))
                        .foregroundColor(.secondary)
                }
            }
            
            Spacer()
        }
        .padding(.vertical, 4)
    }
    
    private func getIconForType(_ type: String) -> String {
        switch type {
        case "链接": return "link"
        case "音乐": return "music.note"
        case "笔记": return "note.text"
        case "图片": return "photo"
        case "视频": return "video"
        default: return "doc"
        }
    }
    
    private func getColorForType(_ type: String) -> Color {
        switch type {
        case "链接": return .blue
        case "音乐": return .orange
        case "笔记": return .green
        case "图片": return .purple
        case "视频": return .red
        default: return .gray
        }
    }
}

// MARK: - 相册页面
struct PhotosView: View {
    @Environment(\.presentationMode) var presentationMode
    
    private let photos = Array(0..<20).map { _ in PhotoItem() }
    private let columns = Array(repeating: GridItem(.flexible(), spacing: 2), count: 3)
    
    var body: some View {
        NavigationView {
            ScrollView {
                LazyVGrid(columns: columns, spacing: 2) {
                    ForEach(photos) { photo in
                        PhotoGridView(photo: photo)
                    }
                }
                .padding(2)
            }
            .navigationTitle("相册")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("完成") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
        }
    }
}

struct PhotoItem: Identifiable {
    let id = UUID()
    let imageName: String
    let colors: [Color]
    
    init() {
        let imageNames = ["photo", "camera", "video", "image", "photo.fill"]
        self.imageName = imageNames.randomElement() ?? "photo"
        
        let colorSets = [
            [Color.blue, Color.purple],
            [Color.green, Color.mint],
            [Color.orange, Color.yellow],
            [Color.red, Color.pink],
            [Color.indigo, Color.cyan]
        ]
        self.colors = colorSets.randomElement() ?? [Color.blue, Color.purple]
    }
}

struct PhotoGridView: View {
    let photo: PhotoItem
    
    var body: some View {
        ZStack {
            LinearGradient(
                gradient: Gradient(colors: photo.colors.map { $0.opacity(0.6) }),
                startPoint: .topLeading,
                endPoint: .bottomTrailing
            )
            .aspectRatio(1, contentMode: .fill)
            .clipped()
            
            Image(systemName: photo.imageName)
                .font(.system(size: 30))
                .foregroundColor(.white.opacity(0.8))
        }
    }
}

// MARK: - 卡包页面
struct CardsView: View {
    @Environment(\.presentationMode) var presentationMode
    
    private let cards = [
        CardItem(title: "招商银行信用卡", number: "**** 1234", color: Color.red),
        CardItem(title: "支付宝", number: "余额 ¥688.88", color: Color.blue),
        CardItem(title: "会员卡", number: "星巴克会员", color: Color.green),
        CardItem(title: "交通卡", number: "北京一卡通", color: Color.orange)
    ]
    
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(spacing: 16) {
                    ForEach(cards) { card in
                        CardItemView(card: card)
                    }
                }
                .padding()
            }
            .navigationTitle("卡包")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("完成") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
        }
    }
}

struct CardItem: Identifiable {
    let id = UUID()
    let title: String
    let number: String
    let color: Color
}

struct CardItemView: View {
    let card: CardItem
    
    var body: some View {
        HStack {
            VStack(alignment: .leading, spacing: 8) {
                Text(card.title)
                    .font(.system(size: 18, weight: .medium))
                    .foregroundColor(.white)
                
                Text(card.number)
                    .font(.system(size: 14))
                    .foregroundColor(.white.opacity(0.8))
            }
            
            Spacer()
            
            Image(systemName: "creditcard.fill")
                .font(.system(size: 24))
                .foregroundColor(.white.opacity(0.7))
        }
        .padding()
        .background(
            LinearGradient(
                gradient: Gradient(colors: [card.color, card.color.opacity(0.7)]),
                startPoint: .topLeading,
                endPoint: .bottomTrailing
            )
        )
        .clipShape(RoundedRectangle(cornerRadius: 12))
    }
}

// MARK: - 表情页面
struct EmojiView: View {
    @Environment(\.presentationMode) var presentationMode
    
    private let emojiPacks = [
        EmojiPack(name: "默认表情", emojis: ["😀", "😃", "😄", "😁", "😆", "😅", "🤣", "😂"]),
        EmojiPack(name: "爱心系列", emojis: ["❤️", "🧡", "💛", "💚", "💙", "💜", "🖤", "🤍"]),
        EmojiPack(name: "手势表情", emojis: ["👍", "👎", "👊", "✊", "🤛", "🤜", "🤞", "✌️"]),
        EmojiPack(name: "动物表情", emojis: ["🐶", "🐱", "🐭", "🐹", "🐰", "🦊", "🐻", "🐼"])
    ]
    
    var body: some View {
        NavigationView {
            List {
                ForEach(emojiPacks) { pack in
                    EmojiPackRow(pack: pack)
                }
            }
            .navigationTitle("表情")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("完成") {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }
        }
    }
}

struct EmojiPack: Identifiable {
    let id = UUID()
    let name: String
    let emojis: [String]
}

struct EmojiPackRow: View {
    let pack: EmojiPack
    
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text(pack.name)
                .font(.system(size: 16, weight: .medium))
                .foregroundColor(.primary)
            
            LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 8), spacing: 8) {
                ForEach(pack.emojis, id: \.self) { emoji in
                    Text(emoji)
                        .font(.system(size: 24))
                        .frame(width: 40, height: 40)
                        .background(Color.gray.opacity(0.1))
                        .clipShape(RoundedRectangle(cornerRadius: 8))
                }
            }
        }
        .padding(.vertical, 8)
    }
}
